<template>
  <div>
    <to-back></to-back>

    <!-- area-list:地区列表
    show-postal：否显示邮政编码
    show-delete 是否显示删除按钮
    show-set-default	是否显示默认地址栏
    show-search-result 是否显示搜索结果
    -->
    <van-address-edit
      class="con"
      :area-list="areaList"
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      :address-info="info"
    >
      <!-- 添加标签  slot-->
      <div class="van-cell van-field">
        <div class="van-cell__title van-field__label">
          <span>标签</span>
        </div>
        <div class="van-cell__value van-field__value">
          <div class="van-field__body">
            <van-radio-group v-model="label" direction="horizontal">
              <van-radio name="家">家</van-radio>
              <van-radio name="公司">公司</van-radio>
              <van-radio name="学校">学校</van-radio>
            </van-radio-group>
          </div>
        </div>
      </div>
    </van-address-edit>
  </div>
</template>

<script>
// 引入地域表
import { areaList } from "@vant/area-data";
import { Toast } from "vant";
import { reqaddressAdd, reqaddressList, reqAddressEdit } from "../../http/http";
export default {
  data() {
    return {
      areaList: areaList,
      label: "家",
      /* areaList: {
        province_list: {
          110000: "北京市",
          120000: "天津市"
        },
        city_list: {
          110100: "北京市",
          120100: "天津市"
        },
        county_list: {
          110101: "东城区",
          110102: "西城区"
          // ....
        } */
      //   },
      searchResult: [],
      // 表单的默认属性值
      info: {}
    };
  },
  methods: {
    // 点击了保存----> 判断是编辑页面，还是新增页面
    onSave(content) {
      console.log(JSON.stringify(content));
      
      // 点击了保存，首先要看地址中有没有id，有id就是编辑，没有id 就是添加
      if (this.$route.query.id) {
        // 编辑
        console.log(content);
        // {"id":42,"uid":"2a8492e0-3af6-11ec-b300-ed9f9961f819","username":"zs","userphone":"12311112222","regioncode":"120101","location":"天津市天津市和平区","useraddress":"222","status":1,"label":"公司"}
        // 地址编辑的请求
        reqAddressEdit({
          id:this.$route.query.id,
          username:content.name,
          userphone:content.tel,
          location: content.province + content.city + content.county,
          useraddress: content.addressDetail,
          status: content.isDefault ? 1 : 0,
          label: this.label

        }).then(res=>{
          if(res.data.code ==200){
            // 跳转页面
            this.$router.push('addressList')
          }

        })
      } else {
        // 添加
        reqaddressAdd({
          // {"name":"zz","tel":"13623524152","country":"","province":"北京市","city":"北京市","county":"东城区","areaCode":"110101","postalCode":"","addressDetail":"111","isDefault":true}
          uid: JSON.parse(localStorage.getItem("user")).uid,
          username: content.name,
          userphone: content.tel,
          regioncode: content.areaCode,
          location: content.province + content.city + content.county,
          useraddress: content.addressDetail,
          status: content.isDefault ? 1 : 0,
          label: this.label
        }).then(res => {
          if (res.data.code == 200) {
            Toast("添加成功");
            // 跳转页面
            this.$router.push("/addressList");
          }
        });
      }
    },
    onDelete() {
      // Toast("delete");
      
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区"
          }
        ];
      } else {
        this.searchResult = [];
      }
    }
  },
  mounted() {
    // 3.进来页面，如果有id，就要获取当前的这条数据的详细信息
    if (this.$route.query.id) {
      reqaddressList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        id: this.$route.query.id
      }).then(res => {
        if (res.data.code == 200) {
          console.log(JSON.stringify(res.data.list[0]));
          // {"id":43,"uid":"2a8492e0-3af6-11ec-b300-ed9f9961f819","username":"wu","userphone":"13322221111","regioncode":"120102","location":"天津市天津市河东区","useraddress":"333","status":1,"label":"学校"}
          let obj = res.data.list[0];
          this.label = obj.label;
          this.info = {
            id: obj.id,
            name: obj.username,
            tel: obj.userphone,
            addressDetail: obj.useraddress,
            areaCode: obj.regioncode,
            isDefault: obj.status == 1
          };
        }
      });
    }
  }
};
</script>

<style scoped>
.con {
  margin-top: 0.8rem;
}
</style>